//
//  MessageListViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/25.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "MessageListViewController.h"
#import "MessageHeaderCell.h"
#import "MessageCell.h"
#import "CustomerServiceViewController.h"
#import "UserInfoModel.h"
#import "MessageListModel.h"
#import "OrderDetailViewController.h"
#import "OrderDetailViewController.h"
#import "ConsignmentOrRecyclingDetailVC.h"
#import "IdentifyAndMaintainDetailVC.h"

NSString * const   MessageHeaderCellIdentifier = @"MessageHeaderCellIdentifier";
NSString * const   MessageCellIdentifier = @"MessageCellIdentifier";
@interface MessageListViewController ()
@property(nonatomic, strong) MessageListModel * model;
@property(nonatomic, copy) NSString * phoneNo;
@property(nonatomic, strong) NSIndexPath * selectedIndexPath;
@end

@implementation MessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupRightItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNormalTableViewNetworkData) name:@"UPDATE_MESSAGE_LIST" object:nil];
}
- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}
- (void)requestPhone{
    
   
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Common/getKfrx" params:nil resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            strongSelf.phoneNo = responseObject[@"data"][@"info"];
        }
    }];
}
//   页面加载调用 子类重写
- (void)getNormalTableViewNetworkData{
    NSLog(@"进入页面请求 子类重写");
    self.currentPageNo = 0;
    self.pageCount = 10;
    
    [self requestMsgList];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self requestMsgList];
}
- (NSInteger )getNetworkTableViewDataCount{
    
    return  [self.model.data count];
}
- (void)requestMsgList{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    [params addEntriesFromDictionary:@{@"p":@(self.currentPageNo)}];
    [params addEntriesFromDictionary:@{@"psize":@(self.pageCount)}];
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Msg/getList" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            if (strongSelf.currentPageNo == 0) {
                 strongSelf.model = [[MessageListModel alloc]initWithDictionary:responseObject error:nil];
                strongSelf.currentPageNo++;
            }else{
                if (responseObject[@"data"] && [responseObject[@"data"] count] >0) {
                    [strongSelf.model.data addObjectsFromArray:responseObject[@"data"]];
                    strongSelf.currentPageNo++;
                }
            }
           
            [strongSelf updateTableView];
        }
        NSLog(@"%@===",responseObject);
        
    }];
}
- (void)setupRightItem{
    
    UIBarButtonItem * barItem = [[UIBarButtonItem alloc]initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(clearList)];
    barItem.tintColor = UIColorFromRGB(0x666666);
    [barItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];
    [barItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateSelected];
    self.navigationItem.rightBarButtonItem = barItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageHeaderCell class]) bundle:nil] forCellReuseIdentifier:MessageHeaderCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageCell class]) bundle:nil] forCellReuseIdentifier:MessageCellIdentifier];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger sections = 1 + [self.model.data count];
    
    return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 1;
    
    return rows;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height =  0;
    if (indexPath.section == 0) {
        height = 60;
    }else{
        height = 110;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.section ==0) {
        MessageHeaderCell * tempCell = [tableView dequeueReusableCellWithIdentifier:MessageHeaderCellIdentifier];
        tempCell.btnBlock = ^(MessageHeaderTouchType type) {
            if (type == MessageHeaderTouchType_phone) {
                [self callPhone:self.phoneNo];
            }else if (type == MessageHeaderTouchType_message){
                [self gotoServiceVC];
            }
        };
        cell = tempCell;
    }else{
        MessageCell * tempCell = [tableView dequeueReusableCellWithIdentifier:MessageCellIdentifier];
        [tempCell setupMessageModel:self.model.data[indexPath.section -1]];
        cell = tempCell;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self gotoServiceVC];
    }else{
        self.selectedIndexPath = indexPath;
        MessageModel * model = self.model.data[indexPath.section -1];
        [self readingMsg:[NSString stringWithFormat:@"%@",model.msg_id]];
  
    }
  
}

- (void)clearList{
    NSDictionary * params = @{@"token":UserInfo.userToken };
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Msg/readAll" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            [strongSelf.tableView reloadData];
        }
    }];
    
}



//客服
- (void)gotoServiceVC{
    CustomerServiceViewController * tempVC = [[CustomerServiceViewController alloc]init];
    [self pushViewController:tempVC];
    
}
- (void)callPhone:(NSString *)mobile{
    
    NSString * callPhone = [NSString stringWithFormat:@"telprompt://%@",mobile];
    
    if (@available(iOS 10.0, *)) {
        /// 大于等于10.0系统使用此openURL方法
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
}


- (void)readingMsg:(NSString *)msg_id{
    NSDictionary * params = @{@"token":UserInfo.userToken,@"msg_id":msg_id };
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Msg/detail" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            MessageModel * model = (MessageModel *)self.model.data[strongSelf.selectedIndexPath.section -1];
            [self gotoVCDetail:model];
           [strongSelf.tableView reloadData];
        }
    }];
    
}
- (void)gotoVCDetail:(MessageModel *)model{
    if([model.type integerValue] == 0 ){
        //-图文
        
        
    }else if ([model.type integerValue] == 1){
        //单品
        
    }else if ([model.type integerValue] == 2){
        //活动会场
        
        
    }else if([model.type integerValue] == 3){
        //订单
        [self gotoOrderDetail:[NSString stringWithFormat:@"%@",model.relation_id]];
    }else if ([model.type integerValue] == 4){
        //鉴定
        [self gotoIdentifyDetail:[NSString stringWithFormat:@"%@",model.relation_id]];
    }
    else if ([model.type integerValue] == 5){
        //养护
        [self gotoMaintainDetail:[NSString stringWithFormat:@"%@",model.relation_id]];
    }
    else if ([model.type integerValue] == 6){
        //寄卖
        [self gotoConsignmentDetail:[NSString stringWithFormat:@"%@",model.relation_id]];
    }
    else if ([model.type integerValue] ==  7){
        //回收
        [self gotoRecyclingDetail:[NSString stringWithFormat:@"%@",model.relation_id]];
    }
}

//订单详情
- (void)gotoOrderDetail:(NSString *)relation_id{
    
    OrderDetailViewController * orderDetailVC = [[OrderDetailViewController alloc]initWithStype:OrderDetailStyle_normal withOrderId:relation_id];
    orderDetailVC.fromVCType = OrderDetailFromVCType_Msg;
    [self pushViewController:orderDetailVC];
}


// -寄卖
- (void)gotoConsignmentDetail:(NSString *)relation_id{
    ConsignmentOrRecyclingDetailVC * detailVC = [[ConsignmentOrRecyclingDetailVC alloc]initWithStyle:ConsignmentOrRecyclingDetailState_normal withVCType:ConsignmentOrRecyclingDetailType_Con withOrderId:relation_id];
    detailVC.fromVCType = ConsignmentOrRecyclingFromVCType_Msg;
    [self pushViewController:detailVC];
}
//回收详情
- (void)gotoRecyclingDetail:(NSString *)relation_id{
    ConsignmentOrRecyclingDetailVC * detailVC = [[ConsignmentOrRecyclingDetailVC alloc]initWithStyle:ConsignmentOrRecyclingDetailState_normal withVCType:ConsignmentOrRecyclingDetailType_Re withOrderId:relation_id];
    detailVC.fromVCType = ConsignmentOrRecyclingFromVCType_Msg;
    [self pushViewController:detailVC];
    
}
//鉴定
- (void)gotoIdentifyDetail:(NSString *)relation_id{
    IdentifyAndMaintainDetailVC * detailVC = [[IdentifyAndMaintainDetailVC alloc]initWithStyle:IdentifyAndMaintainDetailState_normal withVCType:IdentifyAndMaintainDetailType_Identify withOrderId:relation_id];
     detailVC.fromVCType = IdentifyAndMaintainFromVCType_Msg;
    [self pushViewController:detailVC];
    
}
//-养护
- (void)gotoMaintainDetail:(NSString *)relation_id{
    IdentifyAndMaintainDetailVC * detailVC = [[IdentifyAndMaintainDetailVC alloc]initWithStyle:IdentifyAndMaintainDetailState_normal withVCType:IdentifyAndMaintainDetailType_Maintain withOrderId:relation_id];
    detailVC.fromVCType = IdentifyAndMaintainFromVCType_Msg;
    [self pushViewController:detailVC];
    
}
//单品详情页
-(void)gotoSingleDetail{
    
    
}
//活动会场页
- (void)gotoActivity{
    
}
//图文详情页

-(void)gotoGraphicDetail{
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

